/**Trigger Name: NMRLeadAssignTrg
 * Author: Accenture
 * Requirement/Project Name: NMR
 * Requirement Description: Trigger for Assignment of Leads with round robin logic
 */
trigger NMRLeadAssignTrg on Lead (before insert) {
    Public ID NMRLeadRecordType = RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead');
    Public ID NMRLeadCustIdRecordType = RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead with CustomerId');
    List<Lead> leadList = new List<Lead>();  
    List<Lead> marketqueueleadList = new List<Lead>();
    Map<Id,Lead> LeadMap = new Map<Id,Lead>();
    Set<String> ZipStr = new Set<String>();
    Date currentdate = System.today();
    integer numberDaysDue;
        for(Lead leads:Trigger.new){
            
            if((leads.RecordtypeId == NMRLeadRecordType || leads.RecordtypeId == NMRLeadCustIdRecordType) &&(leads.LeadSource == 'AdStore' || leads.LeadSource == 'ASCP')){
                     if(leads.PostalCode != NULL && leads.status != 'No Go – Follow Up'){             
                        leadList.add(leads);
                        LeadMap.put(leads.Id,leads);
                        ZipStr.add(leads.PostalCode);
                        }
                        else{
                            marketqueueleadList.add(leads);
                        }
                   
            }
            
        
        }
    if(leadList.size()>0){
    //Calling the Roundrobin class for newly inserted leads
        NMRLeadRoundRobin.executeRoundRobin(leadList,LeadMap,ZipStr);
    }
    //Assign the non assigned leads to Marketing queue
    if(marketqueueleadList.size()>0){
      Queuesobject nmrQueue = [select QueueId, Queue.Name from Queuesobject where Queue.name = : 'Marketing'];
      for(Lead singlelead:marketqueueleadList){
          singlelead.ownerId = nmrQueue.QueueId;
      }
    }
}